<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * User Application (User), Group List Module
 * Started: 15 October, 2009 7:24 PM
 * Script version: 1.0
 +------------------------------------
 */

class module implements module_interface{
	public function returninitvars(){
		global $lang;
		$this->info["page_title"] = $lang->getByKey("admin.user.groups.list");
		$this->info["page_description"] = "";
		$this->info["module_version"] = "1.0";
		return $this->info;
	}

	public function construct(){ # call our initialize functions
		$this->page = (isset($_GET['page']) ? intval($_GET['page']) : 1);
	}
	
	private function can_delete($data){
		if($data['is_native']){
			return true;
		}
		else return false;
	}
	
	public function run(){
		global $view, $lang, $db, $models, $_CF;
		$_CF->view->writeIn( $_CF->view->inlineInfo($_CF->lang->getByKey("admin.user.groups.list.desc")));
		// Pre-Query Them.
		$groupQuery = $_CF->models->groups->select_all();
		$this->numberOfUsers = $_CF->db->num_rows($groupQuery);
		// Gotta use columns extensively.
		// So first we have to set the pointer to where we *actually* want.
		$x = 0;
		while($x !== ($this->page-1)*20 and $array = $_CF->db->fetch_array($groupQuery)){ $x++; }
		$i = 0;
		$o = 0; // so many temp vars... confused. :(
		while(($data = $_CF->db->fetch_array($groupQuery)) !== FALSE and $i !== 20){
			// Pointy-pointer
			$i++;
			if(!isset($group{$data['permissionmask']})){
				$pm{$data['permissionmask']} = new permissionmask($data['permissionmask']);
			}
			$data['pmaskname'] = $pm{$data['permissionmask']}->getName();
			$data2['del'] = (!$this->can_delete($data) ? 
				$_CF->view->showTemplate("list_del_ls_g", $data) : 
				$_CF->view->showTemplate("list_del_disabled_g", $data));
			// After this infinite loop is over
			$data['del'] = $data2['del'];
			$data['native'] = ($data['is_native'] ? $_CF->lang->getByKey("yes") : $_CF->lang->getByKey("no"));
			$data['admin'] = ($data['is_admin'] ? $_CF->lang->getByKey("yes") : $_CF->lang->getByKey("no"));
			$data['mod'] = ($data['is_smod'] ? $_CF->lang->getByKey("yes") : $_CF->lang->getByKey("no"));
			$output = $_CF->view->tablePadded($data["groupname"],
									  $_CF->view->showTemplate("groupcard", $data),
									  "group");
			if(!isset($columns[$o])){
				$columns[$o]["left"] = $output;
			}
			else{
				if(isset($columns[$o]["left"]) and !isset($columns[$o]["right"])){
					$columns[$o]["right"] = $output;
					$o++;
				}
			}
		}
		// Now yay - Output it.
		for($d=0;($d-1)!==$o;$d++){
			if(!isset($columns[$d]["right"])) $columns[$d]["right"] = "";
			if(!isset($columns[$d]["left"])) $columns[$d]["left"] = ""; // Ugly workaround to get rid of errors.
			$_CF->view->writeIn($_CF->view->showTemplate("column", $columns[$d]));
		}
	}

	public function destruct(){}
}
